package com.task.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.task.dao.ExperienceDao;
import com.task.entity.Experience;
import com.task.entity.User;

public class ExpeAction extends ActionSupport {

	private ExperienceDao  experienceDao;
	
	HttpServletResponse response=null;
	
	private Experience expe;
	
    private File fileData;
	
	private String fileName;
	
	Map session=null;
	
	private String date;
	
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public File getFileData() {
		return fileData;
	}
	public void setFileData(File fileData) {
		this.fileData = fileData;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public Experience getExpe() {
		return expe;
	}
	public void setExpe(Experience expe) {
		this.expe = expe;
	}
	public void setExperienceDao(ExperienceDao experienceDao) {
		this.experienceDao = experienceDao;
	}
	//添加今日总结
	public String doAddExpe() throws Exception{
		session=ActionContext.getContext().getSession();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String date=sdf.format(new Date());	
		expe.setExperienceDate(sdf.parse(date));
		User user1=(User) session.get("LOGINED_USER");
		expe.setUser(user1);
        experienceDao.addExperience(expe);
        response=ServletActionContext.getResponse();
	    response.getWriter().write("1");
        return null;
	}
	//文件上传
	public String doAddFile() throws Exception{
		System.out.println(fileData);
		System.out.println(fileName);
		InputStream is = new FileInputStream(fileData);
		String filepath = ServletActionContext.getServletContext()
				.getRealPath("/upload/");
		String filename = filepath + File.separator + fileName;
		File file = new File(filename);			
		OutputStream os = new FileOutputStream(file);
		int length = 0;
		byte[] b = new byte[1024];
		while ((length = is.read(b)) != -1) {
			os.write(b, 0, length);
		}
		os.close();
		os = null;
		is.close();
		is = null;
		return null;
	}
	//根据时间获取总结
	public String getExpeByDate() throws Exception{
		session=ActionContext.getContext().getSession();
		User user1=(User) session.get("LOGINED_USER");
		Experience expe1=experienceDao.getExperienceByDate(1, date);
		response=ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		StringBuffer buffer=new StringBuffer("[");
		if(expe1==null){
			
		} 
		else{
			 String [] strs=expe1.getFilePath().split(";");
			 for(int i=0;i<strs.length;i++){
				 buffer.append("{content:\""+expe1.getExperienceContent()+"\",path:\""+strs[i].trim()+"\"},");
			 }
		}	
		String result=buffer.toString().substring(0, buffer.toString().length()-1)+"]";
		System.out.println(result);
		response.getWriter().write(result);
		System.out.println(expe1);
		return null;
	}
	//下载附件
	public String doDownFile() throws Exception{
		HttpServletResponse response = ServletActionContext.getResponse();
	    BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		OutputStream fos = null;
		InputStream fis = null;

		// 如果是从服务器上取就用这个获得系统的绝对路径方法。
		ServletContext servletContext = (ServletContext) ActionContext
				.getContext().get(ServletActionContext.SERVLET_CONTEXT);
		String filepath = servletContext.getRealPath("/upload/" +fileName);
		File uploadFile = new File(filepath);
		fis = new FileInputStream(uploadFile);
		bis = new BufferedInputStream(fis);
		fos = response.getOutputStream();
		bos = new BufferedOutputStream(fos);
		// 这个就就是弹出下载对话框的关键代码
		int beginIndex = fileName.lastIndexOf("\\");
		String filename = fileName.substring(beginIndex + 1);
		response.setHeader("Content-disposition", "attachment;filename="
				+ URLEncoder.encode(filename, "utf-8").replace("+", " "));
		int bytesRead = 0;
		// 这个地方的同上传的一样。我就不多说了，都是用输入流进行先读，然后用输出流去写，唯一不同的是我用的是缓冲输入输出流
		byte[] buffer = new byte[1024];
		while ((bytesRead = bis.read(buffer, 0, 1024)) != -1) {
			bos.write(buffer, 0, bytesRead);
		}
		bos.flush();
		fis.close();
		bis.close();
		fos.close();
		bos.close();
		return null;
	}
}
